'''
Created on Nov 8, 2009

@author: mkiyer
'''
import logging

def parse_transcripts_tmap(fhd):
    '''
    ref_gene_id     ref_id  class_code      cuff_gene_id    cuff_id FMI     RPKM    <unknown>    <unknown>    cov     len     major_iso_id
    ['OTTHUMT00000002844', 'OTTHUMT00000002844', 'c', '3064YAAXX_2.1', '3064YAAXX_2.1.0', '100', '0.164120', '0.000000', '0.000000', '0.108248', '76', '3064YAAXX_2.1.0']
    '''
    # first line is header
    fhd.readline()
    for thisline in fhd:        
        if not thisline:
            continue
        if thisline.startswith('#'):
            continue
        thisline = thisline.strip()
        if not thisline:
            continue
        thisfields = thisline.split('\t')
        t = type('TranscriptMap', (object,), dict())
        t.line = thisline
        t.ref_gene_id = thisfields[0] if thisfields[0] is not '-' else None
        t.ref_id = thisfields[1] if thisfields[1] is not '-' else None
        t.class_code = thisfields[2]
        t.cuff_gene_id = thisfields[3]
        t.cuff_id = thisfields[4]
        t.fmi = int(thisfields[5])
        t.rpkm = float(thisfields[6])
        t.cov = float(thisfields[9])
        t.length = int(thisfields[10])
        t.major_iso_id = thisfields[11]
        yield t

def parse_transcripts_refmap(fhd):
    '''
    ref_gene_id     ref_id  class_code      cuff_gene_id    cuff_id FMI     RPKM    <unknown>    <unknown>    cov     len     major_iso_id
    ['OTTHUMT00000002844', 'OTTHUMT00000002844', 'c', '3064YAAXX_2.1', '3064YAAXX_2.1.0', '100', '0.164120', '0.000000', '0.000000', '0.108248', '76', '3064YAAXX_2.1.0']
    '''
    # TODO: not finished
    # first line is header
    fhd.readline()
    for thisline in fhd:        
        if not thisline:
            continue
        if thisline.startswith('#'):
            continue
        thisline = thisline.strip()
        if not thisline:
            continue
        thisfields = thisline.split('\t')
        t = type('TranscriptMap', (object,), dict())
        t.line = thisline
        t.ref_gene_id = thisfields[0] if thisfields[0] is not '-' else None
        t.ref_id = thisfields[1] if thisfields[1] is not '-' else None
        t.class_code = thisfields[2]
        t.cuff_gene_id = thisfields[3]
        t.cuff_id = thisfields[4]
        t.fmi = int(thisfields[5])
        t.rpkm = float(thisfields[6])
        t.cov = float(thisfields[9])
        t.length = int(thisfields[10])
        t.major_iso_id = thisfields[11]
        yield t


def parse_transcripts_expr(fhd):
    '''
    trans_id        bundle_id       chr     left    right   total_score     
    avg_read_score  RPKM    FMI     frac    conf_low        conf_hi coverage
    '''
    line_count = 1
    chunk_count = 0
    debug_every = 10000
    # first line is header
    fhd.readline()
    for thisline in fhd:
        if line_count == debug_every:
            chunk_count += 1
            logging.debug("    %d lines parsed" % (chunk_count * debug_every))
            line_count = 0
        line_count += 1

        if not thisline:
            continue
        if thisline.startswith('#'):
            continue
        thisline = thisline.strip()
        if not thisline:
            continue
        thisfields = thisline.split('\t')
        t = type('TranscriptExpr', (object,), dict())
        t.line = thisline
        t.id = thisfields[0]
        t.chrom = thisfields[2]
        t.start = int(thisfields[3])
        t.end = int(thisfields[4])
        t.rpkm = float(thisfields[7])
        t.fmi = float(thisfields[8])
        t.frac = float(thisfields[9])
        t.conf_low = float(thisfields[10])
        t.conf_high = float(thisfields[11])
        t.cov = float(thisfields[12])
        yield t